﻿using MCPlatform.UI.Controls.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MCPlatform.UI.Controls.Views
{
    /// <summary>
    /// Motion.xaml 的交互逻辑
    /// </summary>
    public partial class Motion : UserControl
    {
        public Motion()
        {
            InitializeComponent();
            JogFButton.AddHandler(Button.MouseDownEvent, new RoutedEventHandler(JogFButton_MouseDown), true);
            JogFButton.AddHandler(Button.MouseUpEvent, new RoutedEventHandler(JogFButton_MouseUp), true);
            JogBButton.AddHandler(Button.MouseDownEvent, new RoutedEventHandler(JogBButton_MouseDown), true);
            JogBButton.AddHandler(Button.MouseUpEvent, new RoutedEventHandler(JogBButton_MouseUp), true);
        }


        public MotionModel MotionModel
        {
            get { return (MotionModel)GetValue(MotionModelProperty); }
            set { SetValue(MotionModelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MotionModelProperty =
            DependencyProperty.Register("MotionModel", typeof(MotionModel), typeof(Motion), new PropertyMetadata(default));

        private void EnableButton_Click(object sender, RoutedEventArgs e)
        {
            if (MotionModel is null || MotionModel.MotionEntity is null) return;          
           // MotionModel.IsEnabled = MotionModel.MotionEntity.IsEnabled;
            if (MotionModel.IsEnabled)
            {
                MotionModel.MotionEntity.Disable();
            }
            else
            {
                MotionModel.MotionEntity.Enable();
            }
           // MotionModel.IsEnabled = MotionModel.MotionEntity.IsEnabled;
        }

        private void JogFButton_MouseDown(object sender, RoutedEventArgs e)
        {
            if (MotionModel is null || MotionModel.MotionEntity is null) return;
            MotionModel.MotionEntity.SetVelocity(MotionModel.SetVelocity);
            MotionModel.MotionEntity.Jog(1);
        }

        private void JogFButton_MouseUp(object sender, RoutedEventArgs e)
        {
            if (MotionModel is null || MotionModel.MotionEntity is null) return;
            MotionModel.MotionEntity.StopMove();
        }


        private void JogBButton_MouseDown(object sender, RoutedEventArgs e)
        {
            if (MotionModel is null || MotionModel.MotionEntity is null) return;
            MotionModel.MotionEntity.SetVelocity(MotionModel.SetVelocity);
            MotionModel.MotionEntity.Jog(0);
        }


        private void JogBButton_MouseUp(object sender, RoutedEventArgs e)
        {
            if (MotionModel is null || MotionModel.MotionEntity is null) return;
            MotionModel.MotionEntity.StopMove();
        }



        private void AbsButton_Click(object sender, RoutedEventArgs e)
        {
            if (MotionModel is null || MotionModel.MotionEntity is null) return;
            MotionModel.MotionEntity.SetVelocity(MotionModel.SetVelocity);
            MotionModel.MotionEntity.AbsMove(MotionModel.SetPositon);
        }

        private void RelButton_Click(object sender, RoutedEventArgs e)
        {
            if (MotionModel is null || MotionModel.MotionEntity is null) return;
            MotionModel.MotionEntity.SetVelocity(MotionModel.SetVelocity);
            MotionModel.MotionEntity.RelMove(MotionModel.SetDistance);
        }

        private void HomeButton_Click(object sender, RoutedEventArgs e)
        {
            if (MotionModel is null || MotionModel.MotionEntity is null) return;
            MotionModel.MotionEntity.SetHomeVel(MotionModel.SetVelocity);
            MotionModel.MotionEntity.HomeMove();
        }

        private void StopButton_Click(object sender, RoutedEventArgs e)
        {
            if (MotionModel is null || MotionModel.MotionEntity is null) return;
            MotionModel.MotionEntity.StopMove();
        }

        private void ResetButton_Click(object sender, RoutedEventArgs e)
        {
            if (MotionModel is null || MotionModel.MotionEntity is null) return;
            MotionModel.MotionEntity.Reset();
        }

        private bool MotionIsNull()
        {
            if (MotionModel is null || MotionModel.MotionEntity is null)
            {
                return true;
            }

            return false;
        }

       

      

        
    }
}
